package sk.vrto.web.views;

import com.vaadin.ui.*;
import sk.vrto.domain.Email;
import sk.vrto.web.ApplicationInjector;
import sk.vrto.web.presenters.EmailsPresenter;

import java.util.ArrayList;
import java.util.List;

public class EmailsView extends VerticalLayout {

    transient private final EmailsPresenter presenter;
    private final int emailLimit;

    // ui
    private List<EmailView> emailViews = new ArrayList<>();
    private int currentPage = 0;
    private Accordion accordion;
    protected Label page;

    public EmailsView(EmailsPresenter presenter, int emailLimit) {
        this.presenter = presenter;
        this.emailLimit = emailLimit;
        presenter.setView(this);

        setWidth("98.5%");
        setSpacing(true);
        addComponent(createAcordion());

        Component pagination = createPagination();
        addComponent(pagination);
        setComponentAlignment(pagination, Alignment.MIDDLE_RIGHT);
    }

    public void incCurrentPage() {
        currentPage++;
    }

    public void decCurrentPage() {
        currentPage--;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int maxPages() {
        return (emailViews.size() + emailLimit - 1) / emailLimit;
    }

    public void updatePageNumber(String val) {
        page.setValue(val);
    }

    public void refreshVisibleTabs() {
        int from = currentPage * emailLimit;
        int to = from + emailLimit;

        for (int i = 0; i < emailViews.size(); i++) {
            if (i < from || i >= to) {
                accordion.getTab(i).setVisible(false);
            } else {
                accordion.getTab(i).setVisible(true);
            }
        }
    }

    public void removeEmailView(EmailView view) {
        accordion.removeTab(accordion.getTab(view));
        emailViews.remove(view);
    }

    private Component createPagination() {
        HorizontalLayout pagination = new HorizontalLayout();
        pagination.setSpacing(true);

        Component previousButton = previousButton();
        pagination.addComponent(previousButton);
        pagination.setComponentAlignment(previousButton, Alignment.MIDDLE_RIGHT);

        Component pageNumber = pageNumber();
        pagination.addComponent(pageNumber);
        pagination.setComponentAlignment(pageNumber, Alignment.MIDDLE_RIGHT);

        Component nextButton = nextButton();
        pagination.addComponent(nextButton);
        pagination.setComponentAlignment(nextButton, Alignment.MIDDLE_RIGHT);

        return pagination;
    }

    private Component pageNumber() {
        page = new Label();
        updatePageNumber(presenter.newPageNumber());
        return page;
    }

    private Component nextButton() {
        Button next = new Button(">");
        next.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                presenter.nextPage();
            }
        });
        return next;
    }

    private Component previousButton() {
        Button previous = new Button("<");
        previous.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                presenter.previousPage();
            }
        });

        return previous;
    }

    private Component createAcordion() {
        accordion = new Accordion();
        for (Email email : presenter.loadEmails()) {
            EmailView emailView = new EmailView(ApplicationInjector.getEmailPresenter(), email);
            emailViews.add(emailView);
            accordion.addTab(emailView, niceCaption(email));
        }
        refreshVisibleTabs();
        return accordion;
    }

    private String niceCaption(Email email) {
        return email.getContact().getName().toUpperCase() + ": " + email.getTitle();
    }

    public int getEmailLimit() {
        return emailLimit;
    }

}
